package com.hsxc.cdz.controller.appController;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hsxc.cdz.base.bean.Bill;
import com.hsxc.cdz.base.bean.MyUser;
import com.hsxc.cdz.trans.bill.BillManager;

import net.sf.json.JSONObject;

/**
 * 账单页面请求
 * 
 * @author 刘力文
 *
 */
@Controller("appBill")
@RequestMapping("/app")
public class BillController {

	/**********************************************************/
	// 自动注入属性
	@Autowired
	BillManager billmanager;

	/**********************************************************/
	// 常量
	public static final int SUCCESS = 0;
	public static final int FAILURE = 1;
	public static final int ERR_USER = 2;

	/**
	 * 根据用户编号获取账单
	 * 
	 * @return
	 */
	@RequestMapping(value = "/bill", method = RequestMethod.GET)
	@ResponseBody
	public Object getBill() {

		JSONObject result = new JSONObject();
		result.put("success", true);
		Subject subject = SecurityUtils.getSubject();
		if (subject == null || subject.getPrincipal() == null) {
			result.put("added", false);
			result.put("errcode", MyUser.NO_LOGIN);
			result.put("message", "当前用户没有登录");
			return result;
		}
		String tel = subject.getPrincipal().toString();
		if (tel == null) {
			result.put("added", false);
			result.put("errcode", ERR_USER);
			result.put("message", "用户信息输入有误");
			return result;
		}
		while (tel.length() < 20) {
			tel += "0";
		}

		List<Bill> list = billmanager.getBillByUserNO(tel);
		if (list == null) {
			result.put("added", false);
			result.put("errcode", FAILURE);
			result.put("message", "未找到账单");
			return result;
		}
		result.put("added", true);
		result.put("data", list);
		return result;
	}
}
